package copyengine.network.roomStructure.subMediator
{
	import copyengine.network.roomStructure.CENetworkCommandNotification;
	import copyengine.network.roomStructure.data.CENetworkRoomMediatorWhiteBoard;
	import copyengine.network.roomStructure.data.CENetworkRoomNeighbourInfoPackage;
	import copyengine.network.roomStructure.executeLayer.CENetworkRoomExecuteHandleNotification;
	import copyengine.utils.debug.CELog;

	import org.puremvc.as3.interfaces.INotification;

	public final class CENetworkRoomSubLockUnLockMediator extends CENetworkRoomSubMediatorBasic
	{
		public function CENetworkRoomSubLockUnLockMediator(_whiteBoard:CENetworkRoomMediatorWhiteBoard)
		{
			super(_whiteBoard);
		}

		override public function listNotificationInterests():Array
		{
			return [
				CENetworkCommandNotification.RECEIVE_COMMAND_LOCK_ROOM,
				CENetworkCommandNotification.RECEIVE_COMMAND_UNLOCK_ROOM,
				CENetworkRoomExecuteHandleNotification.SEND_RECEIVE_INCOMING_DATA,
				];
		}

		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case CENetworkCommandNotification.RECEIVE_COMMAND_LOCK_ROOM:
					doLockRoom();
					break;
				case CENetworkCommandNotification.RECEIVE_COMMAND_UNLOCK_ROOM:
					doUnlockRoom();
					break;
				case CENetworkRoomExecuteHandleNotification.SEND_RECEIVE_INCOMING_DATA:
					onReceiveIncomingData(notification.getBody()["command"],
										  notification.getBody()["commandArg"],
										  notification.getBody()["forwardData"]);
					break;
			}
		}

		//====================
		//==Incoming Data
		//====================

		private function onReceiveIncomingData(_command:uint, _commandArg:Object, _forwardData:Object):void
		{
			switch (_command)
			{
				case CENetworkCommandNotification.S_RESERVE_COMMAND_LOCK_ROOM_RETURN:
					onRoomLockedCallBack(_commandArg);
					break;
				case CENetworkCommandNotification.S_RESERVE_COMMAND_UNLOCK_ROOM_RETURN:
					onRoomUnlockedCallBack(_commandArg);
					break;
			}
		}

		//===========
		//==Lock
		//===========
		private function doLockRoom():void
		{
			//只有房主发送Lock房间命令
			if (whiteBoard.currentRoomMasterUID == whiteBoard.playerUID)
			{
				var lockRoomCommandArg:Object =
					{
						roomID:whiteBoard.roomID,
						uid:whiteBoard.playerUID
					};
				sendSocketCommand(CENetworkCommandNotification.F_RESERVE_COMMAND_LOCK_ROOM, lockRoomCommandArg);
			}
			//暂时不启用P2P加速
//			doSendStartP2PBoosting();
		}

		private function doSendStartP2PBoosting():void
		{
			//===发送P2P加速请求
			var allSocketNeighborIDsArray:Array = [];
			for each (var neighborInfo:CENetworkRoomNeighbourInfoPackage in whiteBoard.roomNeighbourInfoPackageVector)
			{
				//排除玩家自己
				if (neighborInfo.neighbourUID != whiteBoard.playerUID)
				{
					allSocketNeighborIDsArray.push(neighborInfo.neighbourUID);
				}
			}

			sendNotification(CENetworkRoomExecuteHandleNotification.RECEIVE_START_P2P_BOOSTING,
							 {
								 groupName:"GroupRoom_" + whiteBoard.roomID,
								 rtmfpURL:whiteBoard.rtmfpURL,
								 allSocketNeighborIDsArray:allSocketNeighborIDsArray,
								 playerID:whiteBoard.playerUID
							 });
		}

		/**
		 * 只有房主才可以发送锁房间的命令,当发送完命令后 其他玩家会由服务器端发送通知
		 */
		private function onRoomLockedCallBack(_arg:Object):void
		{
			var errorCode:uint = _arg["errorCode"];
			if (errorCode > 0)
			{
				CELog.err("CENetworkRoomSubLockUnLockMediator->onRoomLockedCallBack error code : " + errorCode);
			}
		}

		//===========
		//==Unlock
		//===========

		private function doUnlockRoom():void
		{
			//只有房主发送Lock房间命令
			if (whiteBoard.currentRoomMasterUID == whiteBoard.playerUID)
			{
				var unlockRoomCommandArg:Object =
					{
						roomID:whiteBoard.roomID,
						uid:whiteBoard.playerUID
					};
				sendSocketCommand(CENetworkCommandNotification.F_RESERVE_COMMAND_UNLOCK_ROOM, unlockRoomCommandArg);
			}
			doSendEndP2PBoosting();
		}

		private function doSendEndP2PBoosting():void
		{
			sendNotification(CENetworkRoomExecuteHandleNotification.RECEIVE_END_P2P_BOOSTING);
		}

		/**
		 * 只有房主才可以发送锁房间的命令,当发送完命令后 其他玩家会由服务器端发送通知
		 */
		private function onRoomUnlockedCallBack(_arg:Object):void
		{
			var errorCode:uint = _arg["errorCode"];
			if (errorCode > 0)
			{
				CELog.err("CENetworkRoomSubLockUnLockMediator->onRoomUnlockedCallBack error code : " + errorCode);
			}
		}


	}
}
